Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
TIP: Understanding The Paint Event And The AutoRedraw Property The Paint event is related to the AutoRedraw property. Both Form and Picture Box objects have an AutoRedraw property. If AutoRedraw is set to Off (the default), graphics and text are written only to the screen, and the Paint event is triggered, as indicated in Figure 12.3. The graphics and text are not persistent—they must be redrawn using the Paint event procedure if the object is uncovered (after being covered by another screen element) or restored (after being minimized). If AutoRedraw is set to On, the graphics and text are saved in memory, as well as being written to the screen, and the display is persistent—automatically refreshed from this memory image if the object is restored or uncovered. In this case, the Paint event does not occur. AutoRedraw applies only to graphics and text created with the Line, Circle, Pset, and Text methods; bitmaps and metafiles are persistent regardless of the AutoRedraw setting. Setting AutoRedraw to On slows down screen display of graphics and consumes extra memory. Both of these effects are particularly noticeable with large objects. Figure 12.3 Events associated with loading, showing, sizing, and unloading forms. #### Defining Custom Coordinates If Visual Basic’s assortment of coordinate units doesn’t suit you, define your own. A custom coordinate system can include moving the origin to another location, a useful technique for certain applications. For example, a program that draws maps could set the graphical coordinate units to a meaningful value, such as miles or latitude and longitude. You can also “flip” the Y axis so positive values move upward, making it easier to use the Cartesian coordinates commonly used in graphs. A custom coordinate system affects not only graphics operations, but also the placement of controls in a container. To define a custom coordinate system—which is possible only for Form, Picture Box, and Printer objects—use the Scale method. The syntax is
    objectname.Scale (left, top)-(right, bottom)
where (left, top) are the new coordinates of the left and top of the object, and (right, bottom) are the new coordinates for the right and bottom of the object. For example
    Form1.Scale (0, 0)-(100, 100)
sets a coordinate system where the top-left corner of the object has coordinates (0, 0) and the lower-right corner has coordinates (100, 100). This example does not move the origin, but only changes the measurement unit to be 1/100 of the object dimension. In contrast, the statement
    Form1.Scale (0, 100)-(100, 0)
sets the same measurement units, then moves the origin to the lower-left corner of the object and inverts the Y axis so positive values move upward. When you use Scale to set custom coordinates, Visual Basic automatically changes the object’s ScaleMode property to 0. As you saw in Table 12.1, this indicates that a custom coordinate system is in effect. Applying the Scale method changes the object’s ScaleWidth, ScaleHeight, ScaleLeft, and ScaleTop properties to reflect the new coordinates. For instance, after executing the previous Scale method, the object’s ScaleWidth property would be 100 and its ScaleHeight property would be 100. You can set these properties individually if you want to modify only part of the object’s coordinate system. Thus, the statement
    object.ScaleWidth = 100
results in the object’s horizontal measurement unit being set to 1/100 of its width, while the location of the origin and the vertical measurement unit remain unchanged. Executing the Scale method with no arguments, like this
    object.Scale
will reset the coordinate system to the default (twips with the 0, 0 point at the top left). You must be aware that the Scale method defines its units based on the object’s size at the time the method is executed. Subsequent changes to the object’s size are not automatically taken into account. We’ll see how this works in the custom coordinate demonstration program. This program uses the Circle method in a For...Next loop to draw a series of circles on a form. You’ll learn the details of the Circle method later in the chapter—you needn’t worry about it now. The program consists of a form without controls. Leave all of the form’s properties at their default values and save your work, calling both the form and the project SCALE. Next, place the code shown in Listing 12.3 in the form’s Paint event procedure. Listing 12.3 The Paint event procedure.
    Private Sub Form_Paint()

    Dim i As Integer

    Cls

    For i = 1 To 5
        Circle (150 * i, 100 * i), 50 * i
    Next i

    End Sub
The Circle method draws a circle with its center at the specified coordinates (the values in the parentheses) and with the specified radius. When you execute the program, it will look more or less like Figure 12.4. I say more or less, because the appearance may vary slightly if you made your form a different size than I did or if your display hardware is different from mine. Figure 12.4 The circles drawn with the form’s default coordinate system. After running the program and seeing what the display looks like, add the following statement to the form’s Load event procedure:
    Scale (0, 1000)-(1000, 0)
Run the program again, and you’ll see a quite different display, as shown in Figure 12.5. TIP: Automatic Object References Why is it that we can sometimes refer to properties and methods without specifying an object? For example, you can write simply Scale (0, 1000)-(1000, 0) rather than Form1.Scale (0, 1000)-(1000, 0). In an event procedure, a reference to a property or method automatically refers to the object that received the event, unless another object is specified. Looking at the new display, you’ll see that not only are the circles larger (because the coordinate unit we specified is much larger than a twip), but the circles climb from the bottom of the form rather than descending from the top (because we inverted the Y axis). If you change the size of the form while the program is running, the circles stay the same size—they do not grow or shrink along with the form. Now let’s try one more thing. Copy the Scale (0, 1000)-(1000, 0) statement and paste it in the Paint event procedure, just after the Cls statement. When you run the program and increase the size of the form by dragging its border, the circles are redrawn to keep the same size relative to the form. By resetting the form’s coordinate system each time it is painted, we ensure that the coordinate system units are always defined relative to the form’s current size—not according to its original size.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.